<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = '../uploads/images/';
	private $_folderImageStoreUrl = '/uploads/images/';
	
	function Banner_model()
	{
		parent::Model();	
        $host = $this->db->hostname;
		$arrHost = explode(':',$host);
		$temp =$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	function selfserving_get_all_banners_by_user($status,$fromdate,$todate,$userid,$page,$numrow, $strUserId,$groupid)
	{
	    //echo $status.'<br/>'.$fromdate.'<br/>'.$todate.'<br/>'.$userid.'<br/>'.$page.'<br/>'.$numrow.'<br/>'.$strUserId;
		$data = array();
        $data['info']=array();
		$data['iCount'] = 0;
		$data['pCount'] = 0;
        $CI = &get_instance();
        $CI->load->library('mycommon');
        $lastday = date('d/m',strtotime('-1 day' ,strtotime(date('Y-m-d'))));
        $last2day = date('d/m',strtotime('-2 day' ,strtotime(date('Y-m-d'))));
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_get_all_banners('$status','$fromdate','$todate','$userid','$page','$numrow', '$strUserId','$groupid');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			switch($row['typead'])
			{
				case '0':
					$row['typead'] = 'CPM';
					break;
                case '1':
					$row['typead'] = 'CPC';
					break;
				default:
					break;
			}
			$row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);
            if($row['uviews']!='N/A')
            {
                $row['uviews']=number_format($row['uviews']);
            }
            if($row['clicks']!='N/A')
            {
                $row['clicks']=number_format($row['clicks']);
            }
            if($row['impressions']!='N/A')
            {
                $row['impressions']=number_format($row['impressions']);
            }
			$data['info'][] = $row;
        }
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['pCount'] = $row1[0];
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
    function selfserving_get_inform($userid)
    {
        $data = array();
        $data['table1']=array();
        $data['table2']=array();
        $data['table3']=array();
        $data['table4']=array();
        $this->load->language('banner/banner', $this->_lang);
        $banner_lang=$this->lang->line('banner_lang');
        $todate=date('Y-m-d');
        $fromdate = strtotime ( '-7 day' , strtotime ( $todate ) ) ;
        $fromdate = date ( 'Y-m-d' , $fromdate );
        
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_infom('$userid');") or die(mysqli_error());		
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
            if($row['type']==0)
            {
                $row['link']='';
                $row['title']='';
            }elseif($row['type']==1){
                $link = site_url('campaigndetail/index?campaignid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được tạo';
            }elseif($row['type']==2){
                $link = site_url('campaigndetail/index?campaignid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa đổi tên';
            }elseif($row['type']==3){
                $link = site_url('campaigndetail/index?campaignid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> đang chạy';
            }elseif($row['type']==4){
                $link = site_url('campaigndetail/index?campaignid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa tạm dừng';
            }elseif($row['type']==5){
                $link = site_url('campaigndetail/index?campaignid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được thiết lập ngân sách';
            }elseif($row['type']==6){
                $link = site_url('campaigndetail/index?campaignid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa đổi thời gian chạy';
            }elseif($row['type']==7){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được tạo';
            }elseif($row['type']==8){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được đổi tên';
            }elseif($row['type']==9){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được chạy';
            }elseif($row['type']==10){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa tạm dừng';
            }elseif($row['type']==11){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa đổi đối tượng quảng cáo';
            }elseif($row['type']==12){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa đổi tốc độ';
            }elseif($row['type']==13){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được update';
            }elseif($row['type']==14){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> vừa được duyệt';
            }elseif($row['type']==15){
                $link = site_url('bannerdetail/index?bannerid='.$row['id'].'&fromdate='.$fromdate.'&todate='.$todate);
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900" href="'.$link.'">'.strip_tags($row['name']).'</a> không được duyệt';
            }elseif($row['type']==16){
                $row['title']=$banner_lang['ad'].' <a style="color:#ff9900">'.strip_tags($row['name']).'</a> vừa bị xóa';
            }elseif($row['type']==17){
                $row['title']=$banner_lang['cam'].' <a style="color:#ff9900">'.strip_tags($row['name']).'</a> vừa bị xóa';
            }
			$data['table1'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['table2'][] = $row1;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row3 = mysqli_fetch_array($result))
		{
			$data['table3'][] = $row3;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row3 = mysqli_fetch_array($result))
		{
			$data['table4'][] = $row3;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_banner_update_inline($bannerid,$val,$type)
    {
        $userid=$this->session->userdata('uid');
        $data = array();
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
//		$result = mysqli_query($iconn, "CALL selfserving_banner_update_inline('$bannerid', '$val','$type','$userid');") or show_custom_error(mysqli_error($iconn));
//		mysqli_close($iconn);
        $iconn = $this->db->conn_id;
		$result = mysqli_multi_query($iconn, "CALL selfserving_banner_update_inline('$bannerid', '$val','$type','$userid');");
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $result;
    }
    function selfserving_search_banners($fromdate,$todate,$userid,$name,$page,$numrow)
    {
        $data = array();
        $data['data']=array();
        $data['totalpage']=array();
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_search_banners('$fromdate','$todate','$userid','$name','$page','$numrow');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		$CI = &get_instance();
		$CI->load->library('mycommon');
		while($row = mysqli_fetch_array($result))
		{
            $row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);
			$data['data'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row3 = mysqli_fetch_array($result))
		{
			$data['totalpage'][] = $row3;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_get_popupdomain($bannerid,$typeid,$typedatata,$fromdate,$todate,$status)
    {
        $data = array();
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
//		$result = mysqli_query($iconn, "CALL selfserving_get_popupdomain('$bannerid','$typeid','$typedatata','$fromdate','$todate');") or die(mysqli_error($iconn));
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_popupdomain('$bannerid','$typeid','$fromdate','$todate','$status');") or show_custom_error(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
}
?>